package net.paoding.rose.jade.util;

import net.paoding.rose.jade.excetion.JadeException;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

/**
 * util
 * @author fusheng.zhang
 * @date 2022-02-18 11:58:18
 */
public class JadeUtil {

    /**
     * 将字符串转换为下划线拼接
     * 例如: createTime 转换成  create_time
     * @param key
     * @return
     */
    public static String getKey(String key) {
        return key.replaceAll("([A-Z])", "_$1").toLowerCase();
    }

    /***
     * 反序列化
     * @param bytes
     * @return
     * @param <T>
     */
    @SuppressWarnings("unchecked")
    public static <T> T deserializeObject(byte[] bytes) {
        try (ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
             ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);) {
            return (T) objectInputStream.readObject();
        } catch (Exception e) {
            throw new JadeException(e.getMessage(), e);
        }
    }
}
